package com.cls.business.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cls.business.entity.ReviewDetail;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 专家评审结果详细表 Mapper
 *
 * @author wandering
 * @date 2020-11-18 11:59:01
 */
@Repository
public interface ReviewDetailMapper extends BaseMapper<ReviewDetail> {
    @Select("select indi.name indicatorName, indi.description indicatorDescription,indi.weight, indiDetail.indicator_type, " +
            "indiDetail.question_description, indiDetail.max_score,indiDetail.min_score,viewDetail.score,indiDetail.indicator_detail_id, viewDetail.is_choose " +
            "FROM biz_indicator_detail  indiDetail " +
            "LEFT JOIN biz_indicator indi on indi.indicator_id = indiDetail.indicator_id " +
            "LEFT JOIN biz_review_detail viewDetail on viewDetail.indicator_detail_id = indiDetail.indicator_detail_id  and viewDetail.review_id = #{reviewId} " +
            "WHERE indiDetail.indicator_id in " +
            "(select indicator_id from biz_indicator where standard_id = #{standardId} ) GROUP BY indi.indicator_id , indiDetail.indicator_detail_id")
    List<ReviewDetail> findByStandardAndReviewId(@Param("standardId") Long standardId, @Param("reviewId") Long reviewId);

   @Select("delete from biz_review_detail where review_id=#{reviewId}")
    void deleteForReviewId(@Param("reviewId")Long reviewId);

    @Select("select brd.review_detail_id,brd.review_id,brd.indicator_detail_id ,bi.name indicatorName,\n" +
            "      bi.description indicatorDescription,bi.weight,bid.indicator_type indicatorType,\n" +
            "      bid.question_description questionDescription,bid.max_score maxScore,bid.min_score minScore,\n" +
            "      brd.score,brd.is_choose\n" +
            "  from biz_review_detail brd\n" +
            "      left join biz_indicator_detail bid on brd.indicator_detail_id=bid.indicator_detail_id\n" +
            "      left join biz_indicator bi on bi.indicator_id= bid.indicator_id\n" +
            "  where brd.review_id=#{reviewId} ")
    List<ReviewDetail> getByReviewIdList(Long reviewId);


    @Delete("DELETE from biz_review_detail where review_detail_id in(select review_id from biz_review where plan_id = #{planId} and is_delete = 0)")
    int deleteByPlanId(@Param("planId") Long planId);
}
